﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.GUI.NhanVienBanSach
{
    public partial class TimKiemSach : Form
    {
        DataTable dt = new DataTable();
        public TimKiemSach()
        {
            InitializeComponent();
        }

        private void TimKiemSach_Load(object sender, EventArgs e)
        {
            dt = NhanVienBanSachBUS.DSSach();
            lvDSSach.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaSach"].ToString();
                item.SubItems.Add(dr["TenSach"].ToString());
                item.SubItems.Add(dr["TenTG"].ToString());
                item.SubItems.Add(dr["TenNXB"].ToString());
                item.SubItems.Add(dr["TenTL"].ToString());
                item.SubItems.Add(dr["Gia"].ToString());
                lvDSSach.Items.Add(item);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTenSach.Text == "")
            {
                MessageBox.Show("Tên sách không được trống");
                txtTenSach.Focus();
                return;
            }
            string sql = "SELECT s.MaSach, nxb.TenNXB, s.TenSach, s.Gia, tg.TenTG, tl.TenTL";
            sql += " FROM (((NhaXuatBan nxb INNER JOIN Sach s ON nxb.MaNXB = s.MaNXB) INNER JOIN";
            sql += " TacGia tg ON s.MaTG = tg.MaTG) INNER JOIN TheLoai tl ON s.MaTL = tl.MaTL) WHERE ";
            if(txtTenSach.Text!="")
                sql+="(s.TenSach LIKE '%" + txtTenSach.Text + "%') ";
            if(txtTacGia.Text !="")
                sql += "OR (tg.TenTG LIKE '%" + txtTacGia.Text + "%') ";
            if (txtMaSach.Text != "")
            {
                long temp;
                if (long.TryParse(txtMaSach.Text, out temp) == false)
                {
                    MessageBox.Show("Mã sách phải là ký số");
                    txtMaSach.Text = "";
                    txtMaSach.Focus();
                    return;
                }
                sql += "OR (s.MaSach = " + Convert.ToInt16(txtMaSach.Text) + ") ";
            }
            if(txtNXB.Text!="")
                sql += "OR (nxb.TenNXB LIKE '%" + txtNXB.Text + "%') ";
            if(txtTheLoai.Text!="")
                sql += "OR (tl.TenTL LIKE '%" + txtTheLoai.Text + "%')";

            
            DataTable table = new DataTable();
            table = NhanVienBanSachBUS.TimSach(sql);
            lvDSSach.Items.Clear();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow dr = table.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaSach"].ToString();
                item.SubItems.Add(dr["TenSach"].ToString());
                item.SubItems.Add(dr["TenTG"].ToString());
                item.SubItems.Add(dr["TenNXB"].ToString());
                item.SubItems.Add(dr["TenTL"].ToString());
                item.SubItems.Add(dr["Gia"].ToString());
                lvDSSach.Items.Add(item);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            txtMaSach.Text = "";
            txtNXB.Text = "";
            txtTacGia.Text = "";
            txtTenSach.Text = "";
            txtTheLoai.Text = "";
        }
    }
}